"""
ASGI config for ChatgptServer project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""


import os

from channels.security.websocket import AllowedHostsOriginValidator
from channels.sessions import SessionMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ChatgptServer.settings')
get_asgi_app = get_asgi_application()

from chatgpt.routings import websocket_urlpatterns
from aiapi.urls.dall.urls import websocket_urlpatterns_dall

all_websocket_url = websocket_urlpatterns_dall + websocket_urlpatterns

application = ProtocolTypeRouter({
    'http': get_asgi_app,     # 支持http请求
    # 'websocket': 
    #     URLRouter(routings.websocket_urlpatterns),    # 支持websocket请求

    "websocket": AllowedHostsOriginValidator(
        SessionMiddlewareStack(
            URLRouter(all_websocket_url)
        )
    ),
    
})

